package com.example.aiscripteditorserver.config;

import lombok.extern.slf4j.Slf4j;
import org.opencv.core.Core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;

@Slf4j 
@Configuration
public class OpenCVConfig {


    @Value("${libs.opencv}")
    private String libOpencvPath;

    @PostConstruct
    public void initOpenCV() {
        System.out.println("=== 开始加载 OpenCV 库 ===");
        try {

            // 1. 自动检测操作系统
            String os = System.getProperty("os.name").toLowerCase();
            String fullLibraryPath = libOpencvPath;
            System.out.println("找到 OpenCV 库文件：" + fullLibraryPath);

            // 3. 校验库文件是否存在
            File libraryFile = new File(fullLibraryPath);
            if (!libraryFile.exists()) {
                throw new RuntimeException("OpenCV 库文件不存在！路径：" + fullLibraryPath +
                        "\n请检查项目根目录下的 lib/opencv 目录是否正确放置了对应平台的库文件。");
            }

            // 4. 加载 OpenCV 库
            System.load(fullLibraryPath);
            log.info("OpenCV 库加载成功！版本：" + Core.VERSION);
            log.info("=== OpenCV 初始化完成 ===");

        } catch (Exception e) {
            // 加载失败时终止程序（避免后续功能异常）
            log.error("OpenCV 库初始化异常，程序无法继续运行", e);
        }
    }
    
}